<template>
	<view class="scroll_index">
		<view class="shop_container">
			<view style="width: 100%;height: 40vh;" v-if="params.total ===0">
				<u-empty v-if="machine.mid" :iconSize="80" :textSize="30" mode="data"
					icon="http://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
				<u-empty v-if="machine.mid===''" :iconSize="80" text="请选择机器盒子" :textSize="30" height="50vh" mode="data"
					icon="http://cdn.uviewui.com/uview/empty/data.png">
				</u-empty>
			</view>
			<template v-else-if="!params.list.length===0 && machine.mid">
				<u-skeleton :rows="5" :loading="true" :animate="true"></u-skeleton>
			</template>
			<template v-else>
				<view v-for="(item,index) in params.list" :key="index" @click="handleDetail(item)" class="item">
					<view class="pic_coat">
						<image v-if="item.realProductImgUrl==null"
							src="https://img2.baidu.com/it/u=888733543,2955353211&fm=253&fmt=auto&app=138&f=JPEG?w=748&h=450"
							mode=""></image>
						<image v-else :src="item.realProductImgUrl" mode=""></image>
					</view>
					<view class="content">
						<view class="line"></view>
						<view class="name_container">
							<view class="name">
								{{item.productName}}
							</view>
							<view class="tips">
								棉柔使用|日用
							</view>
						</view>
						<view class="desc">
							超熟睡420柔棉感 量多夜用纤巧棉柔 4片/包124234
						</view>
						<view class="content_price">
							<view class="price">
								￥{{item.price}}
							</view>
						</view>
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	import {
		getShopList
	} from '@/api/index.js'
	export default {
		props: ['keywords', 'machine'],
		mounted() {
			if (this.machine.mid != undefined) {
				this.getList({
					...this.pageParams,
					'mId': this.machine.mid || ""
				})
			}
			uni.$on('withBottom', (val) => {
				if (!this.machine.mid) {
					return
				}
				this.init()
			})
			uni.$on('suffixHandle', (() => {
				this.getSearch({
					'searchName': this.keywords
				})
			}))
			uni.$on('clearHandle', (() => {
				this.pageParams.page = 1
				this.keywords = ""
				this.getList({
					...this.pageParams,
					'mId': this.machine.mid || ""
				})
			}))

			uni.$on('onRehandle', () => {
				this.pageParams.page = 1
				this.getList({
					...this.pageParams,
					'mId': this.machine.mid || ""
				})
			})
		},
		beforeDestroy() {
			uni.$off('withBottom')
		},
		data() {
			return {
				params: {
					list: {},
					total: 0
				},
				pageParams: {
					page: 1,
					limit: 10
				}

			};
		},
		methods: {
			getList(data) {
				getShopList(data).then(res => {
					this.params.list = res.data.list
					this.params.total = res.data.total
				})
			},
			init() {
				if (this.params.total === this.params.list.length) {
					return
				} else {
					this.pageParams.page += 1
					getShopList({
						...this.pageParams,
						'searchName': this.keywords,
						'mId': this.machine.mid || ""
					}).then(res => {
						this.params.list = this.params.list.concat(res.data.list)
					})
				}
			},
			getSearch(val) {
				this.pageParams.page = 1
				this.getList({
					...val,
					...this.pageParams,
					'mId': this.machine.mid || ""
				})
			},
			handleDetail(item) {
				let jsonString = JSON.stringify(item)
				let encodeURIComponentStr = encodeURIComponent(jsonString)
				uni.navigateTo({
					url: `/pages/index/detail/detail?item=${encodeURIComponentStr}`
				})
			}

		},

	}
</script>

<style scoped lang="scss">
	.scroll_index {
		width: 100%;

		.shop_container {
			display: flex;
			justify-content: space-between;
			align-items: center;
			flex-wrap: wrap;
			width: 100%;
			box-sizing: border-box;

			.item {
				padding: 10rpx;
				width: 48%;
				margin: 10rpx 0;
				border-radius: 20rpx;
				box-shadow: 0 0 0.8rem -0.3rem #5a5a5a;
				border-radius: 20rpx;
				background-color: #fff;

				.pic_coat {
					width: 100%;
					margin: 0 auto;
					padding: 15rpx 30rpx;

					image {
						width: 100%;
						height: 160rpx;
					}
				}

				.content {
					padding: 20rpx;

					.line {
						height: 1px;
						width: 100%;
						background-color: #ccc;
					}

					.name_container {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.name {
							margin: 20rpx 0;
							margin-right: 10rpx;
							font-size: 1rem;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							overflow: hidden;
							text-overflow: ellipsis;
							letter-spacing: 0.1rem;
						}

						.tips {
							letter-spacing: 0.1rem;
							color: rgb(189, 189, 189);
							font-size: 0.8rem;
							display: -webkit-box;
							-webkit-box-orient: vertical;
							-webkit-line-clamp: 1;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}


					.desc {
						font-size: 26rpx;
						letter-spacing: 0.15rem;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: normal;
					}

					.content_price {
						display: flex;
						justify-content: space-between;
						align-items: center;

						.price {
							margin: 20rpx 0;
							color: #FF0000;
							font-size: 36rpx;
							font-weight: bold;
						}

						.square {
							height: 30rpx;
							width: 30rpx;
							line-height: 24rpx;
							border: 1px solid #FF0000;
							color: #FF0000;
							text-align: center;
							border-radius: 50%;
						}
					}

				}
			}


		}
	}
</style>